package com.max.back.demo.api;

import cn.hutool.core.bean.BeanUtil;
import com.max.back.common.http.ResponseResult;
import com.max.back.common.sercurity.anno.AuthIgrone;
import com.max.back.demo.entity.DrugType;
import com.max.back.demo.form.DrugTypeDeleteForm;
import com.max.back.demo.form.DrugTypeForm;
import com.max.back.demo.service.DrugTypeSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
/**
 *药品类别控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/api/drugtype")
public class DrugTypeCtl {
    /**
     *药品类别服务接口
     */
    @Autowired
    private DrugTypeSer ser;

    @PostMapping("/add")
    public ResponseResult<Boolean> add(@RequestBody @Validated({DrugTypeForm.Base.class}) DrugTypeForm form) {
        DrugType drugType = BeanUtil.toBean(form, DrugType.class);
        boolean res = ser.checkAndInsert(drugType);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestBody @Validated({DrugTypeForm.Delete.class}) DrugTypeForm form) {
        boolean res = ser.removeById(form.getId());
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/countdelete")
    public ResponseResult<Boolean> countdelete(@RequestBody @Valid DrugTypeDeleteForm form) {
        boolean res = false;
        for (String id : form.getIds()) {
            res = ser.removeById(id);
        }
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody @Validated({DrugTypeForm.Update.class}) DrugTypeForm form) {
        DrugType drugType = BeanUtil.toBean(form, DrugType.class);
        boolean res = ser.updateById(drugType);
        if (res) {
            return ResponseResult.getSuccessResult(true, "A006", null);
        } else {
            return ResponseResult.getMessageResult(false, "A007");
        }
    }

    @PostMapping("/find")
    public ResponseResult<List<DrugType>> find(@RequestBody @Validated({DrugTypeForm.Find.class}) DrugTypeForm form) {
        List<DrugType> drugTypeList = ser.findall(form.getId(), form.getName());
        return ResponseResult.getSuccessResult(drugTypeList);
    }

    @AuthIgrone
    @PostMapping("/gettypelist")
    public ResponseResult<List<DrugType>> getjxlist(@RequestBody @Validated({DrugTypeForm.Gettype.class}) DrugTypeForm form) {
        List<DrugType> list = ser.getjx(form.getSpci());
        return ResponseResult.getSuccessResult(list);
    }
}
